﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Outlook;

namespace ContactsTester
{
    internal sealed class OutlookConnector
    {
        public static event Sync.Events.SyncEvents.ConnectContacts OnConnectContacts;
        public static SyncContact[] LoadOutlookContactDetails(ApplicationClass usersOutlook)
        {
            OnConnectContacts += new Sync.Events.SyncEvents.ConnectContacts(Sync.Events.EventHandlers.OutlookConnector_OnConnectContacts);
            OnConnectContacts(new Sync.Events.ConnectEventArgs(UpdateType.Outlook));
            MAPIFolder fldContacts = (MAPIFolder)usersOutlook.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
            SyncContact[] currentOutlookContacts = new SyncContact[fldContacts.Items.Count];
            int i = 0;
            foreach (ContactItem item in fldContacts.Items)
            {
                CreateOutlookSyncItem(item, currentOutlookContacts, i);
                i++;
            }
            return currentOutlookContacts;
            //ContactItem newContact = (ContactItem)fldContacts.Items.Add(OlItemType.olContactItem);
        }

       

        private static void CreateOutlookSyncItem(ContactItem CurrentOutlookContact, SyncContact[] currentOutlookContactArray, int ContactArrayCount)
        {
            SyncContact updateItem = new SyncContact();
            updateItem.Company = CurrentOutlookContact.CompanyAndFullName;
            updateItem.EmailAddress = CurrentOutlookContact.Email1Address;
            updateItem.FullName = CurrentOutlookContact.FullName;
            updateItem.HomeNumber = CurrentOutlookContact.HomeTelephoneNumber;
            updateItem.MobileNumber = CurrentOutlookContact.MobileTelephoneNumber;
            updateItem.WorkNumber = CurrentOutlookContact.BusinessTelephoneNumber;
            updateItem.SyncType = UpdateType.Outlook;
            updateItem.OriginalOutlookContactItem = CurrentOutlookContact;
            currentOutlookContactArray[ContactArrayCount] = updateItem;
        }
    }
}
